package com.obas.business.aaa.service.impl;

import java.util.Date;
import java.util.List;

import com.ease.common.vo.MapParameter;
import com.ease.common.vo.Pagination;
import com.obas.business.aaa.service.AaaCustomerProcessService;
import com.obas.business.common.support.PaginationHandlerCallback;
import com.obas.model.business.CustomerNetworkServing;
import com.obas.model.business.Device;
import com.obas.model.business.Port;
import com.obas.model.business.Slot;
import com.obas.model.business.Vlan;
import com.obas.model.intermediate.BindAttr;
import com.obas.model.intermediate.editor.BindAttrEditor;
import com.obas.model.metadata.constant.DictionaryConst;
import com.obas.model.metadata.constant.EntityTypeConst;
import com.obas.model.type.BatchTimestampAware;

public class AaaCustomerProcessServiceImpl extends AbstractAaaDataProcessServiceImpl implements AaaCustomerProcessService {

	final static class Statement {

		static String addCustomerByBatchTimestamp = "addCustomerByBatchTimestamp";

		static String addCustomerNetworkServingByBatchTimestamp = "addCustomerNetworkServingByBatchTimestamp";

		static String countAddedCustomerNetworkServing = "countAddedCustomerNetworkServing";

	}

	public void process(final Date batchTimestamp) {
		MapParameter parameter = null;
		{
			parameter = new MapParameter();
			parameter.put(BatchTimestampAware.batchTimestamp, batchTimestamp);
			this.getJdbcTemplateDataAccessObject().execute(statement(Statement.addCustomerByBatchTimestamp), parameter);
		}
		{
			parameter = new MapParameter();
			parameter.put(BatchTimestampAware.batchTimestamp, batchTimestamp);
			this.getJdbcTemplateDataAccessObject().execute(statement(Statement.addCustomerNetworkServingByBatchTimestamp), parameter);
		}

		new PaginationHandlerCallback() {

			public int count() {
				MapParameter parameter = new MapParameter();
				parameter.put(BatchTimestampAware.batchTimestamp, batchTimestamp);
				return AaaCustomerProcessServiceImpl.this.getJdbcTemplateDataAccessObject().count(statement(Statement.countAddedCustomerNetworkServing), parameter);
			}

			public void onPagination(int paginationIndex) {
				// ElementMacAddressProcessServiceImpl.this.concurrentProcessChainService.execute(new
				// ConcurrentRetailCallback<Object>() {
				// public Object execute() throws Throwable {
				AaaCustomerProcessServiceImpl.this.processCustomerNetworkServing(paginationIndex, batchTimestamp);
				// return null;
				// }
				// });

			}
		}.handle();

	}

	public void processCustomerNetworkServing(int paginationIndex, Date batchTimestamp) {
		MapParameter parameter = null;

		Pagination<CustomerNetworkServing> pagination = new Pagination<CustomerNetworkServing>();
		pagination.setIndex(paginationIndex);
		pagination.setSize(PaginationHandlerCallback.TASK_PAGINATION_SIZE);
		// int skipResult = pagination.getStart();
		// int maxResult = pagination.getStart() + pagination.getSize();

		parameter = new MapParameter();
		// parameter.put("skipResult", skipResult);
		// parameter.put("maxResult", maxResult);
		parameter.put(BatchTimestampAware.batchTimestamp, batchTimestamp);
		pagination = this.customerNetworkServingAccessService.findPagination(parameter, pagination);
		List<CustomerNetworkServing> customerNetworkServings = pagination.getItems();

		if (notEmpty(customerNetworkServings)) {
			for (CustomerNetworkServing customerNetworkServing : customerNetworkServings) {
				instantiateBindAttr(customerNetworkServing, batchTimestamp);
			}
		}
	}
	
	public static short[]  INSTANTIATE_BIND_TYPES = new short[]{
		DictionaryConst.AaaCustomer_BindType.VLAN_PORT_SUBSLOT_SLOT_NASIP,//
		DictionaryConst.AaaCustomer_BindType.PORT_SUBSLOT_SLOT_NASIP,//
		DictionaryConst.AaaCustomer_BindType.SVLAN_VLAN_PORT_SUBSLOT_SLOT_NASIP,//
		DictionaryConst.AaaCustomer_BindType.SVLAN_VLAN_VCI_VPI_PORT_SUBSLOT_SLOT_NASIP//
	};
	
	public void instantiateBindAttr(CustomerNetworkServing customerNetworkServing, Date batchTimestamp) {

//		0	不绑定
//		8	vlan+port+subslot+slot+nasip
//		9	vci+vpi+nasip
//		10	port+subslot+slot+nasip
//		11	vci+vpi+port+subslot+slot+nasip
//		12	nasip
//		13	svlan+vlan+port+subslot+slot+nasip
//		14	svlan+vlan+vci+vpi+port+subslot+slot+nasip
//		15	svlan+port+subslot+slot+nasip
		
		if(!contains(customerNetworkServing.getBindType(), INSTANTIATE_BIND_TYPES)){
			return;
		}
		
		// String bindAttr = "svlan=1229||,vlan=1157||,port=11||,subslot=0||,slot=255||,nasip=222.220.255.246||";
		String bindAttr = customerNetworkServing.getBindAttr();

		BindAttrEditor bindAttrEditor = new BindAttrEditor();
		bindAttrEditor.setAsText(bindAttr);
		BindAttr bindAttrObject = bindAttrEditor.getObject();

		//
		String nasip = bindAttrObject.getNasip();
		Device device = null;
		if (notEmpty(nasip)) {
			device = deviceAccessService.findBrasByIpAddress(nasip);
			if (null == device) {
				device = new Device();
				device.setEntityTypeId(EntityTypeConst.BRAS.getId());
				//
				device.setIpAddress(nasip);
				//
				device = deviceAccessService.create(device);
			}
		}

		//
		String slotCode = bindAttrObject.getSlot();
		Slot slot = null;
		if (notEmpty(slotCode)) {
			slot = slotAccessService.findByDeviceIdAndCode(device.getId(), slotCode);
			if (null == slot) {
				slot = new Slot();
				slot.setEntityTypeId(EntityTypeConst.SLOT.getId());
				//
				slot.setCode(slotCode);
				slot.setDeviceId(device.getId());
				//
				slot = slotAccessService.create(slot);
			}
		}

		//
		String subslotCode = bindAttrObject.getSubslot();
		Slot subslot = null;
		if (notEmpty(subslotCode)) {
			subslot = slotAccessService.findSubslotBySlotIdAndCode(slot.getId(), subslotCode);
			if (null == subslot) {
				subslot = new Slot();
				subslot.setEntityTypeId(EntityTypeConst.SUBSLOT.getId());
				//
				subslot.setCode(subslotCode);
				subslot.setDeviceId(device.getId());
				if (null != slot) {
					subslot.setSlotId(slot.getId());
				}
				//
				subslot = slotAccessService.create(subslot);
			}
		}

		//
		String portCode = bindAttrObject.getPort();
		Port port = null;
		if (notEmpty(portCode)) {
			port = portAccessService.findByDeviceIdSlotIdSubSlotIdAndCode(device.getId(), slot.getId(), subslot.getId(), portCode);
			if (null == port) {
				port = new Port();
				port.setEntityTypeId(EntityTypeConst.PORT.getId());
				//
				port.setCode(portCode);
				port.setDeviceId(device.getId());
				if (null != slot) {
					port.setSlotId(slot.getId());
				}
				if (null != subslot) {
					port.setSubslotId(subslot.getId());
				}

				//
				port = portAccessService.create(port);
			}
		}

		//
		if (null != port) {
			String vlanid = bindAttrObject.getVlan();
			Vlan vlan = null;
			if (notEmpty(vlanid)) {
				customerNetworkServing.setCvlan(vlanid);
				customerNetworkServingAccessService.update(customerNetworkServing);
				//
				vlan = vlanAccessService.findByPortIdAndVlanid(port.getId(), vlanid);
				if (null == vlan) {
					vlan = new Vlan();
					vlan.setEntityTypeId(EntityTypeConst.SVLAN.getId());
					//
					vlan.setVlanid(vlanid);
					vlan.setPortId(port.getId());
					//
					vlan = vlanAccessService.create(vlan);
				}
			}
		}
	}

}
